package com.tianqu.tms.entity;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;


import com.tianqu.tms.core.base.TmsEntity;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.dao.ViewListDao;


public abstract class ViewListManager extends TmsEntity {

	/**
	 * Serializable Id
	 */
	private static final long serialVersionUID = 1L;

	/**
	 */
	private int pageNo = 1;
	/**
	 */
	private int maxIndex = 30;
	/**
	 */
	private int sortKey = 2;
	/**
	 */
	private boolean asc = true;
	
	
	/**
	 */
	private int totalCount = 0;
	/**
	 */
	private int dateFrom = 0;
	/**
	 */
	private int dateTo = 0;

	/**
	 * @throws IrisDaoException
	 */
	public void save() throws TmsDaoException {
		// TODO �����������ꂽ���\�b�h�E�X�^�u

	}
	/**
	 * @param idao
	 */
	protected void setPagingInfo(ViewListDao idao){
		this.setDateFrom(idao.getResultFrom());
		this.setDateTo(idao.getResultTo());
		this.setTotalCount(idao.getTotalCount());
		this.setPageNo(idao.getPageNo());
	}
	/**
	 * @return pageNo
	 */
	public int getPageNo() {
		return pageNo;
	}

	/**
	 * @param pageNo the pageNo to set
	 */
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	/**
	 * @return maxIndex
	 */
	public int getMaxIndex() {
		return maxIndex;
	}

	/**
	 * @param maxIndex the maxIndex to set
	 */
	public void setMaxIndex(int maxIndex) {
		this.maxIndex = maxIndex;
	}
	/**
	 * @return totalCount
	 */
	public int getTotalCount() {
		return totalCount;
	}
	/**
	 * @param totalCount the totalCount to set
	 */
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	/**
	 * @return sortKey
	 */
	public int getSortKey() {
		return sortKey;
	}
	/**
	 * @param sortKey the sortKey to set
	 */
	public void setSortKey(int sortKey) {
		this.sortKey = sortKey;
	}
	/**
	 * @return asc
	 */
	public boolean isAsc() {
		return asc;
	}
	/**
	 * @param asc the asc to set
	 */
	public void setAsc(boolean asc) {
		this.asc = asc;
	}
	/**
	 * @return dateFrom
	 */
	public int getDateFrom() {
		return dateFrom;
	}
	/**
	 * @param dateFrom the dateFrom to set
	 */
	public void setDateFrom(int dateFrom) {
		this.dateFrom = dateFrom;
	}
	/**
	 * @return dateTo
	 */
	public int getDateTo() {
		return dateTo;
	}
	/**
	 * @param dateTo the dateTo to set
	 */
	public void setDateTo(int dateTo) {
		this.dateTo = dateTo;
	}
	
	protected List sort(List list, Comparator comparator) {
		if(list.size() == 0 ) {
			return list;
		}
		this.setTotalCount(list.size());
		Object[] objects = list.toArray();
		Arrays.sort(objects, comparator);
		
		List tmp = Arrays.asList(objects);
		
		int dateFrom = (this.getPageNo()-1) * this.getMaxIndex();
		int dateTo = this.getPageNo() * this.getMaxIndex() ;
		if(dateTo > list.size()) {
			dateTo = list.size();
		}
		list = tmp.subList(dateFrom, dateTo);
		
		this.setDateFrom(dateFrom + 1);
		this.setDateTo(dateTo);
		
		return list;
	}

}
